package com.zis.system.server;

import com.alibaba.fastjson.JSON;
import com.zis.starter.core.Constant;
import com.zis.starter.core.ResponseEntity;
import com.zis.starter.core.SearchParams;
import com.zis.system.dao.DictTypeDao;
import com.zis.system.inter.IDictionaryTypeInter;
import com.zis.system.model.DictionaryType;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Create by wangshen 2018/1/31
 */
@Service
@Transactional("txManager")
public class DictTypeInterImpl implements IDictionaryTypeInter {

    @Autowired
    private DictTypeDao dictTypeDao;


    @Override
    public ResponseEntity insert(DictionaryType model) throws Exception {
        int num = dictTypeDao.getSQLManager().insert(model);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    @Override
    public ResponseEntity update(DictionaryType model) throws Exception {
        int num = dictTypeDao.getSQLManager().updateTemplateById(model);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    @Override
    public ResponseEntity delete(String id) throws Exception {
        int num = dictTypeDao.getSQLManager().deleteById(DictionaryType.class, id);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    @Override
    public ResponseEntity selectById(String id) throws Exception {
        DictionaryType result = dictTypeDao.unique(id);
        if (result != null) {
            return ResponseEntity.success(JSON.parseObject(JSON.toJSONString(result)));
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_NULL);
    }

    @Override
    public PageQuery<DictionaryType> findByPage(SearchParams params) throws Exception {
        PageQuery<DictionaryType> query = new PageQuery<>();
        query.setPageNumber(params.getPage());
        query.setPageSize(params.getPageSize());
        query.setParas(params.getParams());
        dictTypeDao.getSQLManager().pageQuery("system.dictionaryType.query", DictionaryType.class, query);
        return query;
    }

    @Override
    public ResponseEntity findByCondition(Map params) throws Exception {
        List<DictionaryType> resultList = dictTypeDao.getSQLManager().select("system.dictionary.query", DictionaryType.class, params);
        if (resultList != null && resultList.size() > 0) {
            return ResponseEntity.success(JSON.parseArray(JSON.toJSONString(resultList)));
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_NULL);
    }
}
